# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openshadinglanguage

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.4.1
pkgrel=1
pkgdesc="Advanced shading language for production GI renderers (mingw-w64)"
arch=('any')
url="https://github.com/imageworks/OpenShadingLanguage/"
license=("custom")
depends=("${MINGW_PACKAGE_PREFIX}-openexr"
    "${MINGW_PACKAGE_PREFIX}-boost"
    "${MINGW_PACKAGE_PREFIX}-libpng" 
    "${MINGW_PACKAGE_PREFIX}-glew"
    "${MINGW_PACKAGE_PREFIX}-libtiff"
    "${MINGW_PACKAGE_PREFIX}-openimageio"
    "${MINGW_PACKAGE_PREFIX}-freetype"
    #"${MINGW_PACKAGE_PREFIX}-intel-tbb"
    "${MINGW_PACKAGE_PREFIX}-llvm=3.4"
    "${MINGW_PACKAGE_PREFIX}-ilmbase"
    "${MINGW_PACKAGE_PREFIX}-openexr"
    )
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake" "${MINGW_PACKAGE_PREFIX}-clang=3.4" "${MINGW_PACKAGE_PREFIX}-gcc")
source=(https://github.com/imageworks/OpenShadingLanguage/archive/Release-${pkgver}.tar.gz
    0001-mingw-w64-fix.patch
    0002-dont-add-flex-include-dir.patch
    0003-fix-OIIO-windows-and-remove-unused-var.patch
#    0004-use-find_package-for-LLVM.patch
    0005-Fix-cast-from-void-ptr.patch
    0006-Fix-weird-array-out-of-bounds-error.patch
#    0100-llvm32.patch
    0101-Clean-up-bitcode-generation--including-3-4-fixes.patch)
md5sums=('653da56205360a668a6f71637b00a4c1'
         'c225e66cd0a59a674b331b26374ba082'
         '78526dd22cea1d42238609a9cdfef816'
         '58beefd98884fa278734edf25afb5985'
#         '87fb9dd7fc41c24162eabf67307f8662'
         '73851a8ec414f099fe1195ea18d756f9'
         '33d03ef6eb6433c0df111098a6c7b177'
#         '3147f26453b28a971688ab6e54933449'
         '590137809ea344b41b1ad1e0fc81738c')

prepare() {
  cd ${srcdir}/OpenShadingLanguage-Release-$pkgver
  patch -p1 -i ${srcdir}/0001-mingw-w64-fix.patch
  patch -p1 -i ${srcdir}/0002-dont-add-flex-include-dir.patch
  patch -p1 -i ${srcdir}/0003-fix-OIIO-windows-and-remove-unused-var.patch
#  patch -p1 -i ${srcdir}/0004-use-find_package-for-LLVM.patch
  patch -p1 -i ${srcdir}/0005-Fix-cast-from-void-ptr.patch
  patch -p1 -i ${srcdir}/0006-Fix-weird-array-out-of-bounds-error.patch
#  patch -p1 -i ${srcdir}/0100-llvm32.patch
  patch -p1 -i ${srcdir}/0101-Clean-up-bitcode-generation--including-3-4-fixes.patch
}

build() {
  mkdir -p ${pkgdir}${MINGW_PREFIX}
  pushd ${pkgdir}${MINGW_PREFIX} > /dev/null
  export PREFIX_WIN=`pwd -W`
  popd > /dev/null
  
  pushd ${MINGW_PREFIX} > /dev/null
  export PREFIX_DEPS=`pwd -W`
  popd > /dev/null
  
  export OPENIMAGEIOHOME=${MINGW_PREFIX}
  export LLVM_DIRECTORY=${MINGW_PREFIX}
  cd ${srcdir}/OpenShadingLanguage-Release-$pkgver/src
  [[ -d build-${CARCH} ]] && rm -r build-${CARCH}
  mkdir build-${CARCH} && cd build-${CARCH}
  # DEXTRA_OSLEXEC_LIBRARIES is from "llvm-config --ldflags", OSL uses 
  # "llvm-config --libfiles" but that doesn't include all of them.
    ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX:PATH=${PREFIX_WIN} \
    -DILMBASE_CUSTOM=True \
    -DILMBASE_CUSTOM_INCLUDE_DIR=${PREFIX_DEPS}/include \
    -DILMBASE_CUSTOM_LIB_DIR=${PREFIX_DEPS}/lib \
    -DILMBASE_CUSTOM_LIBRARIES="Half Iex Imath IlmThread" \
    -DEXTRA_OSLEXEC_LIBRARIES="-lz -lffi -lncurses -lshell32 -lpsapi -limagehlp -lm" \
    -DLLVM_STATIC=1 \
    -DVERBOSE=1 \
    -DCMAKE_CXX_FLAGS="-DTINYFORMAT_ALLOW_WCHAR_STRINGS" \
      ../..
    # $PWD/src/liboslcomp PATH addition needed for running oslc.exe during the build (for liboslcomp.dll)
    # ${MINGW_PREFIX}/bin PATH addition needed for finding libboost_system-mt.dll when running oslc.exe. 
    PATH=$PWD/src/liboslcomp:${MINGW_PREFIX}/bin:"${PATH}" make VERBOSE=1
}

package() {
  cd ${srcdir}/OpenShadingLanguage-Release-$pkgver/src/build-${CARCH}

  make install

  mkdir -p "${pkgdir}${MINGW_PREFIX}"/share/OSL/
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/share/licenses/$pkgname
  mv "${pkgdir}${MINGW_PREFIX}"/LICENSE "${pkgdir}${MINGW_PREFIX}"/share/licenses/$pkgname # TODO: Tell upstream about this shit
  mv "${pkgdir}${MINGW_PREFIX}"/{CHANGES,README.md,INSTALL} "${pkgdir}${MINGW_PREFIX}"/share/OSL/
  mv "${pkgdir}${MINGW_PREFIX}"/doc "${pkgdir}${MINGW_PREFIX}"/share/OSL/doc
  mv "${pkgdir}${MINGW_PREFIX}"/shaders "${pkgdir}${MINGW_PREFIX}"/share/OSL/shadersE
}
